package com.example.exception;

import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Jonathan
 * 2021/4/24 12:07
 * 全局异常处理
 */
@ControllerAdvice
@Order(0)
public class GlobalExceptionHandler {


    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public ResponseEntity<ErrorMsg> handleHttpException(HttpServletRequest req, BaseException e) {
        ExceptionEnum exceptionEnum = e.getExceptionEnum();
        String errorCode = exceptionEnum.getErrorCode();
        String errorMsg = exceptionEnum.getErrorMsg();
        HttpStatus httpStatus = exceptionEnum.getHttpStatus();
        return new ResponseEntity<>(new ErrorMsg(errorCode, errorMsg), httpStatus);

    }

}
